package com.young.shop.api.controller;

import com.sun.org.apache.xpath.internal.objects.XString;
import com.young.shop.api.service.OrderService;
import com.young.shop.pojo.entity.Goods;
import com.young.shop.pojo.entity.Order;
import com.young.shop.pojo.vo.BaseRespVO;
import com.young.shop.pojo.vo.OrderServiceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Api(tags = "订单业务接口")
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController{
    @Autowired
    OrderService orderServiceImpl;

    @PostMapping("/mount")
    @ApiOperation("加载订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", value = "用户id", dataType = "Long")
    })
    public BaseRespVO mount( @RequestParam("userId")Long userId){
        try {
            List<Order> orders = orderServiceImpl.mountByUserId(userId);
            List<Order> orderServiceVOS = removeGoodsByStatus(orders);
            return convertSuccessResult(orderServiceVOS);
        }catch (RuntimeException e){
            String errorMsg = e.getMessage();
            return convertErrorResult(errorMsg);
        }
    }

    @GetMapping("/chang")
    @ApiOperation("修改订单状态修改为已付款")
    public void chang(HttpServletRequest request,HttpServletResponse response) throws Exception{
        System.out.println("/chang 修改订单状态修改为已付款");
        String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
        String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
        try {
            orderServiceImpl.PaymentOrderByOrderId(out_trade_no);
        }catch (Exception e){
            e.getMessage();
        }
        response.sendRedirect("http://localhost:8080//view-api/order.html");
    }

    @PostMapping("/chang02")
    @ApiOperation("修改订单状态修改为已付款")
    public void chang02(HttpServletRequest request,HttpServletResponse response) throws Exception{
        System.out.println("/chang 修改订单状态修改为已付款");
        String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
        String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
        try {
            orderServiceImpl.PaymentOrderByOrderId(out_trade_no);
        }catch (Exception e){
            e.getMessage();
        }
    }

    @PostMapping("/state")
    @ApiOperation("查询状态订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "status", value = "订单状态", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "userId", value = "用户id", dataType = "Long")
    })
    public BaseRespVO status(@RequestParam("status") int status,@RequestParam("userId") Long userId){
        try {
            List<OrderServiceVO> state = orderServiceImpl.queryByStatus(userId, status);
            return convertSuccessResult(state);
        }catch (RuntimeException e){
            return convertErrorResult(e.getMessage());
        }
    }

    @PostMapping("/particulars")
    @ApiOperation("查询订单详情")
    @ApiImplicitParam(paramType = "query", name = "orderId", value = "订单id", dataType = "Long")
    public BaseRespVO particulars(@RequestParam("orderId") Long orderId){
        try {
            List<OrderServiceVO> orderServiceVOS = orderServiceImpl.particularsByOrderId(orderId);
            return convertSuccessResult(orderServiceVOS);
        }catch (RuntimeException e){
            return convertErrorResult(e.getMessage());
        }
    }
    @GetMapping("/refund")
    @ApiOperation("订单退款接口")
    public void refund(String orderId){
//        System.out.println("aaa");
        int i = orderServiceImpl.refundOrderByOrderId(orderId);
//        System.out.println("修改："+i);
    }

    @PostMapping("/cancellation")
    @ApiOperation("申请取消订单")
    @ApiImplicitParam(paramType = "query",name = "orderId",value = "订单id",dataType = "Long")
    public BaseRespVO cancellation(@RequestParam("orderId") Long orderId){
        int i = orderServiceImpl.cancellationOrderByOrderId(orderId);
        return convertSuccessResult(i);
    }

    @PostMapping("applyForRefund")
    @ApiOperation("申请订单退款接口")
    @ApiImplicitParam(paramType = "query",name = "orderId",value = "订单id",dataType = "Long")
    public BaseRespVO applyForRefund(@RequestParam("orderId") Long orderId){
        try {
            int i = orderServiceImpl.applyForRefund(orderId);
            BaseRespVO baseRespVO = convertSuccessResult(i);
            return baseRespVO;
        }catch (RuntimeException e){
            String message = e.getMessage();
            return convertErrorResult(message);
        }

    }

    @PostMapping("deleteOrder")
    @ApiOperation("删除订单接口")
    @ApiImplicitParam(paramType = "query",name = "orderId",value = "订单id",dataType = "Long")
    public BaseRespVO deleteOrder(@RequestParam("orderId") Long orderId){
        try {
            int i = orderServiceImpl.deleteOrder(orderId);
            return convertSuccessResult(i);
        }catch (RuntimeException e){
            String message = e.getMessage();
            return convertErrorResult(message);
        }
    }

    @PostMapping("confirmReceipt")
    @ApiOperation("确认收货接口")
    @ApiImplicitParam(paramType = "query",name = "orderId",value = "订单id",dataType = "Long")
    public BaseRespVO confirmReceipt(@RequestParam("orderId") Long orderId){
        try {
            int i = orderServiceImpl.confirmReceipt(orderId);
            return convertSuccessResult(i);
        }catch (RuntimeException e){
            String message = e.getMessage();
            return convertErrorResult(message);
        }
    }


    public List<Order> removeGoodsByStatus(List<Order> list){
        for (int i = 0; i < list.size(); i++) {
           if (list.get(i).getStatus() == 0){
               list.remove(i);
               --i;
           }
        }
        return list;
    }




}
